<template>
  <div class="stock-fixation-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>库存锁定</span>
          <el-button type="primary" @click="handleCreate">新建锁定</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="物料编码">
          <el-input
            v-model="queryForm.materialCode"
            placeholder="请输入物料编码"
          />
        </el-form-item>
        <el-form-item label="锁定状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="已锁定" value="locked" />
            <el-option label="已解锁" value="unlocked" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="materialCode" label="物料编码" />
        <el-table-column prop="materialName" label="物料名称" />
        <el-table-column prop="quantity" label="锁定数量" />
        <el-table-column prop="reason" label="锁定原因" />
        <el-table-column prop="operator" label="操作人" />
        <el-table-column prop="lockTime" label="锁定时间" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag
              :type="scope.row.status === 'locked' ? 'danger' : 'success'"
            >
              {{ scope.row.status === "locked" ? "已锁定" : "已解锁" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button
              v-if="scope.row.status === 'locked'"
              link
              type="primary"
              @click="handleUnlock(scope.row)"
            >
              解锁
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新建锁定对话框 -->
    <el-dialog v-model="dialogVisible" title="新建库存锁定" width="500px">
      <el-form :model="lockForm" label-width="100px">
        <el-form-item label="物料编码" required>
          <el-input
            v-model="lockForm.materialCode"
            placeholder="请输入物料编码"
          />
        </el-form-item>
        <el-form-item label="锁定数量" required>
          <el-input-number v-model="lockForm.quantity" :min="1" />
        </el-form-item>
        <el-form-item label="锁定原因" required>
          <el-input
            v-model="lockForm.reason"
            type="textarea"
            placeholder="请输入锁定原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleLockConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

const queryForm = reactive({
  materialCode: "",
  status: "",
});

const tableData = ref([
  {
    materialCode: "M001",
    materialName: "物料A",
    quantity: 100,
    reason: "质量检查",
    operator: "张三",
    lockTime: "2024-03-20 10:00:00",
    status: "locked",
  },
]);

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);

const dialogVisible = ref(false);
const lockForm = reactive({
  materialCode: "",
  quantity: 1,
  reason: "",
});

const handleSearch = () => {
  // 实现查询逻辑
};

const handleReset = () => {
  queryForm.materialCode = "";
  queryForm.status = "";
};

const handleCreate = () => {
  dialogVisible.value = true;
  Object.assign(lockForm, {
    materialCode: "",
    quantity: 1,
    reason: "",
  });
};

const handleLockConfirm = () => {
  // 实现锁定逻辑
  ElMessage.success("锁定成功");
  dialogVisible.value = false;
};

const handleUnlock = (row) => {
  ElMessageBox.confirm("确认解锁该物料库存吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    ElMessage.success("解锁成功");
  });
};

const handleSizeChange = (val) => {
  pageSize.value = val;
  // 重新加载数据
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  // 重新加载数据
};
</script>

<style scoped>
.stock-fixation-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
